package com.good.child.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.good.child.MainActivity;
import com.good.child.R;
import com.good.child.bean.constants.ConstantsBean;
import com.good.child.config.Constants;
import com.good.child.http.LoginLogical;
import com.good.child.iview.SettingNicknameIView;
import com.good.child.pressent.SettingNicknamePresenter;
import com.good.child.widgets.editext.ClearEditText;
import com.yimidida.library.base.BaseActivity;
import com.yimidida.library.base.ICallBackResultListener;
import com.yimidida.library.base.LibraryApplication;
import com.yimidida.library.tool.TActivityUtils;
import com.yimidida.library.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create on: 2024/10/17
 * by Mr.Zhu
 * Description: TODO
 */
public class SettingInformationActivity extends BaseActivity<SettingNicknameIView, SettingNicknamePresenter> implements SettingNicknameIView {

    @BindView(R.id.bog_layout)
    ConstraintLayout boyLayout;
    @BindView(R.id.girl_layout)
    ConstraintLayout girlLayout;
    @BindView(R.id.shop_ed)
    ClearEditText nicknameEdit;
    @BindView(R.id.submit_btn)
    Button saveBtn;

    private int defaultSex = 0;//默认男宝


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activtiy_setting_information);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected SettingNicknamePresenter initPresenter() {
        return new SettingNicknamePresenter(this);
    }

    @Override
    protected void initView() {
        setStatusBar(true, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setAndroidNativeLightStatusBar(this, true);
        setViewLineVisable(false);
        setTitle("设置宝宝资料");

    }

    @Override
    public void onSuccess(List<ConstantsBean> dataList) {

    }

    @OnClick({R.id.bog_layout, R.id.girl_layout, R.id.submit_btn})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.bog_layout:
                selectSex(0);
                break;
            case R.id.girl_layout:
                selectSex(1);
                break;
            case R.id.submit_btn:
                onSubmit();
                break;
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private void selectSex(int sex) {
        if (sex == 0) {//男宝
            boyLayout.setBackground(getResources().getDrawable(R.drawable.shape_rect_fd6900_radius22));
            girlLayout.setBackground(getResources().getDrawable(R.drawable.shape_rect_b5b5bb_radius22));
        } else if (sex == 1) {//女宝
            boyLayout.setBackground(getResources().getDrawable(R.drawable.shape_rect_b5b5bb_radius22));
            girlLayout.setBackground(getResources().getDrawable(R.drawable.shape_rect_fd6900_radius22));
        }
        defaultSex = sex;
    }

    private void onSubmit() {
        String nickname = nicknameEdit.getText().toString();
        if (TextUtils.isEmpty(nickname)) {
            ToastUtil.showShort("请输入宝宝昵称");
            return;
        }
        LoginLogical.getInstance().requestSettingUserName(this, nickname, defaultSex, new ICallBackResultListener() {
            @Override
            public void onCallBack(Object result) {
                if (result == null) {
                    return;
                }
                LibraryApplication.getSharedata().saveData(Constants.FIRST_LOGIN, false);
                LibraryApplication.getSharedata().saveData(Constants.USER_NAME, nickname);
                TActivityUtils.jumpToActivity(SettingInformationActivity.this, MainActivity.class);
                finish();
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            TActivityUtils.jumpToActivity(getBaseContext(), MainActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
